/**   
* @Title: StartMain.java 
* @Package web 
* @Description: TODO(用一句话描述该文件做什么) 
* @author jacking  
* @date 2018年7月26日 上午9:02:15 
* @version V1.0   
*/
package com.reps;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

import org.yaml.snakeyaml.Yaml;

import com.reps.service.DowloadThread;

/**
 * @ClassName: StartMain
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author jacking
 * @date 2018年7月26日 上午9:02:15
 * 
 */

public class RepsStartMain {
	public static Properties properties = new Properties();
	public static String dtHostPort;// 183.203.128.38:10013
	public static String dtUserName;// 140221106000
	public static String dtjssesionid;// 14c2k81xjqsai
	public static String dtwinid;// dx_845_
	public static String dtUserStart;
	public static String dtUserEnd;
	public static String jobSecond;
	public static String dtSaveFilePath;// F:/阳高县/东小村镇/西小村卫生室.txt;
	public static String preData = "row_num;name;sex;person_number;family_num;birthday;physical_examination_time;duty_doctor;symptom;symptom_other;temperature;puls_rate;breath_rate;blood_left_low;blood_left_high;blood_right_low;blood_right_high;weight;height;physical_fitness_index;waist;recognition_ability;intelligence_score;emotion_state;delete1;depressed_score;heath_self_evaluate;ability_self_evaluate;delete2;poison_dust;has_dust_measure;dust_measure;delete3;poison_radiate;has_radiate_measure;radiate_measure;delete4;poison_physical_reason;has_physical_reason_measure;physical_reason_measure;delete5;poison_chem;has_chem_measure;chem_measure;delete6;poison_other;has_other_measure;other_measure;training_rate;training_time_minite;training_year;training_way;eat_habit;cigarette_every_day;smoke_start_year;year_stop_cigaratte;drink_rate;dirnk_size;if_stop_drink;stop_drink_age;start_drink_age;if_drink_oneyear;drink_type;drink_type_other;has_occupation_disease;work_type;start_work_time;lips;teeth;throat;left_eye;right_eye;correct_left_eye;correct_right_eye;hearing;traing;eye_ground;eye_ground_exception;skin;skin_exception;sclera;sclera_exception;lymph;lymph_exception;chest;breath_voice;breath_voice_exception;rale;rale_other;heart_rate;heart_rhythm;heart_voice;heart_voice_remark;tenderness;tenderness_remark;mass;mass_remark;hepa;hepa_remark;splen;splen_remark;voice;voice_remark;edema;arterial;anus;anus_remark;breast;breast_remark;vulva;vulva_remark;vagina;vagina_remark;cervix;cervix_remark;corpus;corpus_remark;gynecology_attachement;gynecology_attachement_exception;gynecology_other;cruorin;leukocyte;platelet;routine_blood_other;routine_urine_test;routine_urine_test_other;blood_sugar_value1;blood_sugar_value2;cardiogram;delete7;cardiogram_exception;delete8;urine_albumen;delete9;shit_blood;delete10;ghb;delete11;hbsag;sgpt;sgot;liver_albumin;tbil;dbil;scr;blood_urea_nitrogen;blood_potassium_contentration;blood_sodium_concentration;total_cholesterol;triglyceride;serum_low_density;serum_high_density;chest_x_pic;chest_x_pic_remark;b_mode;b_mode_remark;cervical_smear;cervical_smear_remark;accessory_examination_other;constitution_identification;brain_blood_disease;brain_blood_disease_other;kidney_disease;kidney_disease_other;heart_disease;heart_disease_other;vessel_disease;vessel_disease_other;eye_disease;eye_disease_other;nervous_disease;nervous_disease_other;other_system_disease;other_system_disease_other;hos_in_time1;hos_out_time1;hos_reason1;hos_name1;hos_patient_id1;hos_in_time2;hos_out_time2;hos_reason2;hos_name2;hos_patient_id2;create_bed_time1;cancel_bed_time1;bed_reason1;bed_hos_name1;bed_patientid1;create_bed_time2;cancel_bed_time2;bed_reason2;bed_hos_name2;bed_patientid2;medicine_name1;medicine_use1;medicine_capacity1;medicine_time1;medicine_comply1;medicine_name2;medicine_use2;medicine_capacity2;medicine_time2;medicine_comply2;medicine_name3;medicine_use3;medicine_capacity3;medicine_time3;medicine_comply3;medicine_name4;medicine_use4;medicine_capacity4;medicine_time4;medicine_comply4;medicine_name5;medicine_use5;medicine_capacity5;medicine_time5;medicine_comply5;medicine_name6;medicine_use6;medicine_capacity6;medicine_time6;medicine_comply6;vaccinate_name1;vaccinate_time1;vaccinate_institution1;vaccinate_name2;vaccinate_time2;vaccinate_institution2;vaccinate_name3;vaccinate_time3;vaccinate_institution3;has_health_exception;health_exception1;health_exception2;health_exception3;health_exception4;health_advice;dangerous_factors;weight_aim;advice_vaccinate;health_other";

	// 开始抓取用户下标，从0开始
	private static Integer start = 0;
	// 总人数
	private static Integer length = 0;

	static {
	/*	InputStream in = RepsStartMain.class.getClassLoader().getResourceAsStream("config.properties");
		try {
			properties.load(new InputStreamReader(in, "utf-8"));
			dtHostPort = properties.getProperty("dtHostPort");
			dtUserName = properties.getProperty("dtUserName");
			dtjssesionid = properties.getProperty("dtjssesionid");
			dtwinid = properties.getProperty("dtwinid");
			dtUserStart = properties.getProperty("dtUserStart");
			dtUserEnd = properties.getProperty("dtUserEnd");
			jobSecond = properties.getProperty("jobSecond");
			dtSaveFilePath = properties.getProperty("dtSaveFilePath");
			start = Integer.parseInt(dtUserStart);
			length = Integer.parseInt(dtUserEnd);
			System.out.println(properties.getProperty("dtSaveFilePath"));
		} catch (IOException e) {
			e.printStackTrace();
		}
*/
		Map propMap = new Yaml().loadAs(RepsStartMain.class.getResourceAsStream("/application.yml"), HashMap.class);// 如果读入Map,这里可以是Mapj接口,默认实现为LinkedHashMap
		dtHostPort = propMap.get("dtHostPort").toString();
		dtUserName = propMap.get("dtUserName").toString();
		dtjssesionid = propMap.get("dtjssesionid").toString();
		dtwinid = propMap.get("dtwinid").toString();
		dtUserStart =propMap.get("dtUserStart").toString();
		dtUserEnd = propMap.get("dtUserEnd").toString();
		jobSecond = propMap.get("jobSecond").toString();
		dtSaveFilePath = propMap.get("dtSaveFilePath").toString();
		start = Integer.parseInt(dtUserStart);
		length = Integer.parseInt(dtUserEnd);
		
//		183.203.128.38:10013, 140221105000,5j4fx5gkf1vv,dx_1088_,229,376,F:/阳高县/东小村镇/神泉寺卫生室.txt
//		System.out.println(dtHostPort);
	}

	public synchronized static Integer getNext() {
		if (start < length)
			return start++;
		else
			return null;
	}

	private static void init() {
		jobSecond = "5";
		dtHostPort = "183.203.128.38:10013";
		dtUserName = "140221105000";
		dtjssesionid = "1hm8pd3nzt7s4";
		dtwinid = "dx_1646_";
		dtUserStart = "0";
		dtUserEnd = "376";
		dtSaveFilePath = "C:/Users/Administrator/Desktop/dataNew-bin/dataNew/tt.txt";

		start = Integer.parseInt(dtUserStart);
		length = Integer.parseInt(dtUserEnd);
	}

	 public static boolean isInteger(String str) {  
	        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
	        return pattern.matcher(str).matches();  
	  }
	public static void main(String[] args) throws InterruptedException {
		// init();

	 	int threadCount = 1;
		if (args.length > 0&&args.length<2) {
			if(isInteger(args[0])){
				threadCount = Integer.parseInt(args[0]);
			}
		}else{
			if(args.length==2){
				if(isInteger(args[0])){
					threadCount = Integer.parseInt(args[0]);
				}
				String [] data=args[1].split(",");
				if(data.length==7){
//					183.203.128.38:10013, 140221105000,5j4fx5gkf1vv,dx_1088_,229,376,F:/阳高县/东小村镇/神泉寺卫生室.txt
					dtHostPort = data[0];
					dtUserName = data[1];
					dtjssesionid = data[2];
					dtwinid = data[3];
					dtUserStart = data[4];
					dtUserEnd = data[5];
					dtSaveFilePath = data[6];

					start = Integer.parseInt(dtUserStart);
					length = Integer.parseInt(dtUserEnd);
				}
			
			}
		}
		for (Integer i = 0; i < threadCount; i++) {
			synchronized (i) {
				int next = getNext();
				DowloadThread thread = new DowloadThread(next, next + 1, "线程" + i);
				thread.start();
				Thread.sleep(5000);
			}

		}

	}

}
